package com.caowei.weicimpress.weicimpress;

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;

import java.util.Stack;

/**
 * Created by caoweiPc on 2014/7/16.
 */
public class ActivityManager {
    private Stack<ActionBarActivity> activities=new Stack<ActionBarActivity>();
    private static ActivityManager manager;
    private ActivityManager(){

    }

    public static ActivityManager getInstance(){
        synchronized (ActivityManager.class){
            if(manager==null){
                manager=new ActivityManager();
            }
        }
        return manager;
    }

    public synchronized void push(ActionBarActivity activity){
        activities.push(activity);
    }

    public synchronized ActionBarActivity popTop(Activity activity){
        if(activities.peek()!=null){
            if(activity.equals(activities.peek())){
                return popTop();
            }
        }
        return null;
    }

    public synchronized ActionBarActivity popTop(){
        return activities.pop();
    }
    public synchronized void finishTop(){
        Activity activity=activities.pop();
        activity.finish();
    }

    public synchronized void finishAll(){
        for(Activity activity:activities){
            finishTop();
        }
    }

    public ActionBarActivity peekTop(){
        return activities.peek();
    }
}
